//格式化时间
function dateFtt(fmt,date)   
{ //author: meizz   
  var o = {   
    "M+" : date.getMonth()+1,                 //月份   
    "d+" : date.getDate(),                    //日   
    "h+" : date.getHours(),                   //小时   
    "m+" : date.getMinutes(),                 //分   
    "s+" : date.getSeconds(),                 //秒   
    "q+" : Math.floor((date.getMonth()+3)/3), //季度   
    "S"  : date.getMilliseconds()             //毫秒   
  };   
  if(/(y+)/.test(fmt))   
    fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));   
  for(var k in o)   
    if(new RegExp("("+ k +")").test(fmt))   
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
  return fmt;   
} 
function formatSubscribeTime(cellValue, options, rowObject) {
	var dateStr = "";
	if(cellValue){
		var crtTime = new Date(cellValue);
		dateStr = dateFtt("yyyy-MM-dd hh:mm:ss",crtTime);
	}
    return dateStr;
}
//加载字典
function dictJudge(value, options, row){
    //sex为自定义字典项
	var statusObj = vm.options.sex;
    for( var i=0;i< statusObj.length;i++ ){
        if(statusObj[i].value == value){
            return '<span>'+statusObj[i].text+'</span>' ;
        }
    }
}
$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'message/message/list',
        datatype: "json",
        colModel: [			
			{ label: 'ID号', name: 'id', index: 'id', width: 15, key: true },
			{ label: '用户名', name: 'username', index: 'username', width: 30 },
			{ label: '消息类型', name: 'type', index: 'type', width: 35,
				formatter: function(cellvalue, options, rowObject){
					if (cellvalue == 0 ){
						return "普通消息";
					}
					if (cellvalue == 1){
						return "推送单条消息";
					}
					if (cellvalue == 2){
						return "推送全部设备";
					}
					if (cellvalue == 3){
						return "普通消息Excel导入";
					}
				}
			},
			{ label: '消息标题', name: 'title', index: 'title', width: 30 },
			{ label: '消息内容', name: 'message', index: 'message'},
			{ label: '读取状态', name: 'readstate', index: 'readstate', width: 20,
				formatter: function (cellvalue, options, rowObject) {
					return cellvalue == 1? "已读":"未读";
                }
			}
			// { label: '', name: 'createdate', index: 'createdate', width: 80 },
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
	    q:{
            messageType:'',
            username:''
        },
		title: null,
		message: {
        },

        excelType:'',

        //显示页面
		showList: true,
        addMessageView: false,
        addPushAllView: false,
        updateView: false
	},
	methods: {
		query: function () {
			vm.reload();
		},
		addMessage: function(){
			vm.showList = false;
			vm.addMessageView = true;
            $("#readstateUpdate").hide();

            vm.title = "新增消息";
			vm.message = {
			    type:''   //默认选择
            };
		},
        addPushAll: function(){
		    vm.showList = false;
		    vm.addPushAllView = true;
            $("#readstateUpdate2").hide();
            vm.title = "新增全部推送";
            vm.message = {
                type:'2'
            };

        },
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.message.id == null ? "message/message/save" : "message/message/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.message),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});


		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "message/message/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "message/message/info/"+id, function(r){
                vm.message = r.message;

                if (vm.message.type == 0 || vm.message.type == 1) {
                    vm.addMessageView = true;
                    $("#readstateUpdate").show();
                }
                if (vm.message.type == 2){
                    vm.addPushAllView = true;
                    $("#readstateUpdate2").show();
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
			vm.addMessageView = false;
			vm.addPushAllView = false;
			vm.updateView = false;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    'type': vm.q.messageType,
                    'username': vm.q.username
                },
                page:page
            }).trigger("reloadGrid");
		}
	}
});